﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
namespace StateManager
{
    class GameOverScreen : GenericScreenCode
    {
        // this class creates a message box that tells the user that the game is over.
        // When the player hits A or enter, the player is taken back to the main menu.

        string message;
        Texture2D box;
        public event EventHandler<PlayerEventHandler> okThen;

        public GameOverScreen(string message)
            : this(message, true)
        {

        }

        public GameOverScreen(string message, bool includeUsage)
        {
            const string usageText = "\n Press A or Enter to Continue";


            if (includeUsage)
                this.message = message + usageText;
            else
                this.message = message;

            IsPopup = true;
        }

        public override void LoadContent()
        {
            //base.LoadContent();
            ContentManager content = SManager.Game.Content;

            box = content.Load<Texture2D>("images/ConfirmBox");
        }

        public override void HandleInput(Input input)
        {
            //base.HandleInput(input);
            PlayerIndex pIndex;
            //base.HandleInput(input);

            //if (input.IsMenuSelect(ControllingPlayer, out pIndex))
            //{
            //    if (yes != null)
            //        yes(this, new PlayerEventHandler(pIndex));

            //    ExitScreen();
            //}
            if (input.IsMenuSelect(ControllingPlayer, out pIndex))
            {
                if (okThen != null)
                    okThen(this, new PlayerEventHandler(pIndex));

                //ExitScreen();
                LoadScreen.Load(SManager, true, null, new BackgroundScreen(), new TitleScreen());
            }
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);
            SpriteBatch spriteBatch = SManager.SpriteBatch;
            SpriteFont font = SManager.SpriteFont;

            Viewport view = SManager.GraphicsDevice.Viewport;
            Vector2 viewSize = new Vector2(view.Width, view.Height);
            Vector2 textSize = font.MeasureString(message);
            Vector2 textPosition = (viewSize - textSize) / 2;

            Rectangle backgroundRect = new Rectangle((int)textPosition.X,
                                                     (int)textPosition.Y,
                                                     (int)textSize.X,
                                                     (int)textSize.Y);

            spriteBatch.Begin();

            spriteBatch.Draw(box, backgroundRect, Color.White);

            spriteBatch.DrawString(font, message, textPosition, Color.White);

            spriteBatch.End();
        }

        
    }
}
